/*
 * Copyright (c) 2009. Silenus Consultoria, S.L.
 */
package es.silenus.detecta.olap;

import es.silenus.detecta.model.Context;

import mondrian.olap.Member;
import mondrian.olap.MemberFormatter;

import java.util.Locale;


/**
 * Base class for member formatters.
 *
 * @author <a href="mailto:malonso@silenus-consultoria.es">Mariano Alonso</a>
 *
 * @since 14-abr-2009 20:21:21
 */
public class BaseMemberFormatter implements MemberFormatter {
/**
   * Constructor.
   */
	public BaseMemberFormatter() {
	}

	/**
	 * Retrieves the key.
	 *
	 * @param member the member.
	 *
	 * @return the key.
	 */
	protected String getKey(final Member member) {
		if(member != null) {
			return member.getName();
		}

		return null;
	}

	/**
	 * Returns the string to be displayed as a caption for a given member.
	 *
	 * @param member the member.
	 *
	 * @return the caption.
	 */
	public String formatMember(final Member member) {
		String key = getKey(member);
		Locale userLocale = Context.getLocale();
		String result = Context.getMessageSource().getMessage(key, null, null, userLocale);

		if((result == null) && (member != null)) {
			result = member.getName();
		}

		return result;
	}
}
